<template>
  <div>
      <div>
        <el-button class="right" type="primary" @click="dialogFormVisible1 = true">新增</el-button>
        <el-form :inline="true" :model="input1" class="demo-form-inline">
          <el-form-item label="角色名称">
            <el-input v-model="input1.name"></el-input>
          </el-form-item>
          <el-form-item label="启用状态">
            <el-select v-model="input1.state" clearable>
              <el-option label="正常" value="1"></el-option>
              <el-option label="停用" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="selectByPage(pageInfo.current,pageInfo.size)">查询</el-button>
          </el-form-item>
          <el-form-item >
            <el-dialog title="新增" :visible.sync="dialogFormVisible1" :modal-append-to-body=false width="60%">
              <el-form :inline="true" :model="addList">
                <el-form-item label="角色编号" :label-width="formLabelWidth1">
                  <el-input v-model="addList.roleCode" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="名称" :label-width="formLabelWidth1">
                  <el-input v-model="addList.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="启用状态" :label-width="formLabelWidth">
                  <el-select v-model="addList.state">
                    <el-option label="正常" :value="1"></el-option>
                    <el-option label="停用" :value="2"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="dialogFormVisible1 = false,addRole(addList)">确 定</el-button>
              </div>
            </el-dialog>
          </el-form-item>
        </el-form>
      </div>
      <el-table
          :data="pageInfo.records"
          border
          style="width: 100%">
        <el-table-column
            prop="roleCode"
            label="角色编号"
            width="180">
        </el-table-column>
        <el-table-column
            prop="name"
            label="角色名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="user.name"
            label="创建人">
        </el-table-column>
        <el-table-column
            prop="updateTime"
            label="更新时间">
        </el-table-column>
        <el-table-column  label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state == 1">正常</span>
            <span v-else>停用</span>
          </template>
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="updRoleList(scope.row)" >修改</el-button>
            <el-dialog title="角色信息修改" :visible.sync="dialogFormVisible" :modal-append-to-body=false width="50%">
              <el-form :inline="true" :model="roleList">
                <el-form-item label="id" :label-width="formLabelWidth">
                  <el-input :disabled="true" v-model="roleList.id" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色编号" :label-width="formLabelWidth">
                  <el-input v-model="roleList.roleCode" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="名称" :label-width="formLabelWidth">
                  <el-input v-model="roleList.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="启用状态" :label-width="formLabelWidth">
                 <el-select v-model="roleList.state">
                    <el-option label="正常" :value="1"></el-option>
                    <el-option label="停用" :value="2"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogFormVisible = false,updRole(roleList)">确 定</el-button>
              </div>
            </el-dialog>
            |
            <el-button type="text" @click="del(scope.row.id)">删除</el-button>
            |
            <el-button type="text" @click="updCheck(scope.row)">权限分配</el-button>
            <el-dialog  :before-close="clearTree" title="分配权限" :visible.sync="dialogFormVisible2" :modal-append-to-body=false width="50%">
              <el-tree
                  :data="checkList"
                  show-checkbox
                  :default-expanded-keys="expandedKeys"
                  node-key="id"
                  :props="defaultProps"
                  :check-strictly="checkStrictly"
                  ref="permTree">
              </el-tree>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="dialogFormVisible2 = false,updPage(scope.row.id)">确 定</el-button>
              </div>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page=this.pageInfo.current
          :page-sizes="[5,10]"
          :page-size=this.pageInfo.size
          layout="sizes, prev, pager, next, jumper,->,total"
          :total=this.pageInfo.total>
      </el-pagination>
    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  name: "Jsgl",
  data:function (){
    return{
      checkStrictly:true,
      expandedKeys:[],
      pageInfo:{
        size:5
      },
      dialogFormVisible : false,
      dialogFormVisible1:false,
      dialogFormVisible2:false,
      roleList:{},
      formLabelWidth:'120px',
      formLabelWidth1:'100px',
      input1:{
        name:'',
        state:''
      },
      addList:{
        state:1
      },
      checkList: [],
      checkListTemp:[],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  methods:{
    clearTree(done){
      this.checkList=[];
      this.expandedKeys=[];
      done();
    },
    /*changeTreeNodeStatus(node) {
      node.expanded = this.checkList;
      for(let i = 0; i < node.childNodes.length; i++ ) {
        //改变节点的自身expanded状态
        node.childNodes[i].expanded = this.checkList;
        //看看他孩子的长度，有的话就调用自己往下找
        if(node.childNodes[i].childNodes.length > 0) {
          this.changeTreeNodeStatus(node.childNodes[i]);
        }
      }
    },*/
    selectByPage(current,size){
      this.$axios.post('/nb/role/getRole/'+current+'/'+size,this.input1).then(res=>{
        this.pageInfo=res.data.data;
      })
    },
    handleSizeChange(val) {
       this.selectByPage(this.pageInfo.current,val);
    },
    handleCurrentChange(val) {
       this.selectByPage(val,this.pageInfo.size);
    },
    updRoleList(res){
      this.dialogFormVisible=true
      this.roleList=res
    },
    updRole(res){
      this.$axios.post('/nb/role/updrole',res).then(res=>{
        this.$message({
          type: 'success',
          message: '更新成功!'
        });
        this.selectByPage(this.pageInfo.current,this.pageInfo.size);
      })
    },
    addRole(res){
     this.$axios.post('/nb/role/addrole',res).then(res=>{
       this.$message({
         type: 'success',
         message: '新增成功!'
       });
       this.selectByPage(this.pageInfo.current,this.pageInfo.size);
      })
    },
    del(roleId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(roleId)
        this.$axios.post('/nb/role/delrole/'+roleId).then(res=>{
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.selectByPage(this.pageInfo.current,this.pageInfo.size);
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    updCheck(res){
      this.checkList=this.checkListTemp;
      this.dialogFormVisible2=true;
      this.expandedKeys=res.permsId;
      this.$nextTick(()=>{
        this.$refs.permTree.setCheckedKeys(res.permsId);
        this.checkStrictly=false
      })
    },
    getTreeData(){
      this.axios({
        method:'post',
        url:'/nb/page/getAll/menu'
      }).then((res)=>{
        this.checkList=res.data.data;
        this.checkListTemp=res.data.data;
      })
    },
    updPage(roleId){
      this.axios({
        method:'post',
        url:'/nb/role-page/add/'+roleId+'/'+this.$refs.permTree.getCheckedKeys()
      }).then((res)=>{
        this.selectByPage(this.pageInfo.current,this.pageInfo.size)
        this.$message({
          type: 'success',
          message: '更新成功!'
        });
      })
    }
  },
  created(){
    this.selectByPage(1,5);
    this.getTreeData();
  }
}
</script>

<style scoped>
  .demo-form-inline{
    display: flex;
  }
  .right{
    display: flex;
    float: right;
  }
</style>
